package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.IMrBedService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MrWard;
import com.ruoyi.system.service.IMrWardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网关Controller
 *
 * @author ruoyi
 * @date 2023-04-01
 */
@RestController
@RequestMapping("/system/ward")
public class MrWardController extends BaseController
{
    @Autowired
    private IMrWardService mrWardService;

    @Autowired
    private IMrBedService mrBedService;

    /**
     * 查询网关列表
     */
    @PreAuthorize("@ss.hasPermi('system:ward:list')")
    @GetMapping("/list")
    public TableDataInfo list(MrWard mrWard)
    {
        startPage();
        List<MrWard> list = mrWardService.selectMrWardList(mrWard);
        return getDataTable(list);
    }

    /**
     * 导出网关列表
     */
    @PreAuthorize("@ss.hasPermi('system:ward:export')")
    @Log(title = "网关", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MrWard mrWard)
    {
        List<MrWard> list = mrWardService.selectMrWardList(mrWard);
        ExcelUtil<MrWard> util = new ExcelUtil<MrWard>(MrWard.class);
        util.exportExcel(response, list, "网关数据");
    }

    /**
     * 获取网关详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ward:query')")
    @GetMapping(value = "/{wardId}")
    public AjaxResult getInfo(@PathVariable("wardId") Long wardId)
    {
        return success(mrWardService.selectMrWardByWardId(wardId));
    }

    /**
     * 新增网关
     */
    @PreAuthorize("@ss.hasPermi('system:ward:add')")
    @Log(title = "网关", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MrWard mrWard)
    {
        return toAjax(mrWardService.insertMrWard(mrWard));
    }

    /**
     * 修改网关
     */
    @PreAuthorize("@ss.hasPermi('system:ward:edit')")
    @Log(title = "网关", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MrWard mrWard)
    {
        return toAjax(mrWardService.updateMrWard(mrWard));
    }

    /**
     * 删除网关
     */
    @PreAuthorize("@ss.hasPermi('system:ward:remove')")
    @Log(title = "网关", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wardIds}")
    public AjaxResult remove(@PathVariable Long[] wardIds)
    {
        int bandBed = mrBedService.checkWardBandBed(wardIds);
        if(bandBed > 0){
            return error("该网关下有通道，无法删除！");
        } else {
            return toAjax(mrWardService.deleteMrWardByWardIds(wardIds));
        }
    }
}
